// pages/payResult/payResult.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp();
Page({
  data: {
    status: false,
    orderId: 0
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.orderId || 24,
      status: options.status
    })
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  payOrder() {
    pay.payOrder(parseInt(this.data.orderId)).then(res => {
      this.setData({
        status: true
      });
    }).catch(res => {
      util.showErrorToast('支付失败');
    });

    util.request(api.PayPrepayId, {
      orderId: orderId
    }).then(function(res){
      console.log(res);
      if (res.errno === 0) {
        const payParam = res.data;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
           success: function (e) {
             console.log("success");
             console.log(e);
          },
           fail: function (e) {
             console.log("fail");
             console.log(e);
          },
           complete: function (e) {
             console.log("complete");
             console.log(e);
             if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") {//支付失败转到待支付订单列表
               wx.showModal({
                 title: "提示",
                 content: "订单尚未支付",
                 showCancel: false,
                 confirmText: "确认",
                 success: function (res) {
                   if (res.confirm) {
                     wx.redirectTo({
                       url: "/pages/order/order?status=1",
                     });
                   }
                 }
               });
               return;
             }
             wx.redirectTo({
               url: "/pages/order/order?status=2",
             });
          }
        });
      }
    });

  }
})